<template>
  <div class="add-depart">
    <el-dialog :title="mytitle" :visible="showDialog" :before-close="cancel">
      <el-form
        ref="myfrom"
        :model="formData"
        label-width="140px"
        :rules="rules"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" style="width:80%" />
        </el-form-item>
        <el-form-item label="部门编号" prop="code">
          <el-input v-model="formData.code" style="width:80%" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <!-- <el-input v-model="formData.manager" style="width:80%" ></el-input> -->
          <el-select v-model="formData.manager" placeholder="请选择">
            <el-option
              v-for="item in peoples"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" style="width:80%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  getDepartmentById,
  updateDepartments
} from '../../../api/departments'
import { getEmployeeSimple } from '../../../api/employees'

export default {
  props: {
    showDialog: {
      type: Boolean
    },
    currentObj: Object
  },
  data() {
    return {
      peoples: [],
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: async(rule, value, callback) => {
              const res = await getDepartments()
              //   console.log(res.depts)
              // 这里的this如果不用箭头函数this指向window
              //   console.log(this.currentObj)
              if (!this.formData.id) {
                var bool = res.depts
                  .filter(v => {
                    return v.pid === this.currentObj.id
                  })
                  .some(v => {
                    return v.name === value
                  })
              } else {
                bool = res.depts
                  .filter(v => {
                    return (
                      v.pid === this.currentObj.pid &&
                      v.id !== this.currentObj.id
                    )
                  })
                  .some(v => {
                    return v.name === value
                  })
              }
              //   console.log(result)
              if (!bool) {
                callback()
              } else {
                callback(new Error('当前部门已有同名部门'))
              }
            }
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: async(rule, value, callback) => {
              const res = await getDepartments()
              //   console.log(res.depts)
              // 这里的this如果不用箭头函数this指向window
              //   console.log(this.currentObj)
              if (!this.formData.id) {
                var bool = res.depts.some(v => {
                  return v.code === value
                })
              } else {
                bool = res.depts.some(v => {
                  return v.code === value && v.id !== this.currentObj.id
                })
              }
              //   console.log(result)
              bool ? callback(new Error('当前编号和其他部门重名')) : callback()
            }
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符'
          }
        ]
      }
    }
  },
  computed: {
    mytitle() {
      return !this.formData.id ? '新增部门' : '编辑部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    async getDepartmentById(id) {
      const res = await getDepartmentById(id)
      //   console.log(res)
      this.formData = res
      // console.log(this.formData.id)
    },
    cancel() {
      this.$emit('update:showDialog', false)
      this.$refs.myfrom.resetFields()
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
        // id: ''
      }
    },
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    submit() {
      this.$refs.myfrom.validate(async res => {
        if (res) {
          //   const result=
          if (!this.formData.id) {
            await addDepartments({
              ...this.formData,
              pid: this.currentObj.id
            })
          } else {
            await updateDepartments(this.formData)
          }
          this.$message({
            type: 'success',
            message: '操作部门成功'
          })
          //   console.log(result)
          this.$emit('update:showDialog', false)
          this.$emit('refresData')

          this.formData = {
            name: '', // 部门名称
            code: '', // 部门编码
            manager: '', // 部门管理者
            introduce: '' // 部门介绍
            // id: ''
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
// .add-depart {
.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
}
// }
</style>
